/**
 * Copyright (C) 2011 Daniel Centore
 * 
 * This file is part of WriteIn.
 *
 * WriteIn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WriteIn is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WriteIn.  If not, see <http://www.gnu.org/licenses/>.
 *  
 */
package in.write.data.stored;

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import javax.swing.text.StyledEditorKit.BoldAction;

/**
 * Handles a user's preferences
 * 
 * @author Daniel Centore
 * 
 */
public class PreferenceManager
{
	public static final String RESOLUTION = "RESOLUTION";
	public static final String DEFAULT_WIDTH = "DEFAULT_WIDTH";
	public static final String DEFAULT_HEIGHT = "DEFAULT_HEIGHT";
	
	private static Preferences prefs;

	static
	{
		prefs = Preferences.userRoot().node("in/write/v1");
	}
	
	/**
	 * Gets a value
	 * @param key The key to get the value based on
	 * @return The value, or null if key did not exist
	 */
	public static String get(String key)
	{
		return prefs.get(key, null);
	}
	
	/**
	 * Puts in a value
	 * @param key The key to put it under
	 * @param value The value to give it
	 */
	public static void put(String key, String value)
	{
		prefs.put(key, value);
		flush();
	}
	
	/**
	 * Gets a value
	 * @param key The key to get the value based on
	 * @return The value, or -1 if key did not exist
	 */
	public static int getInt(String key)
	{
		return prefs.getInt(key, -1);
	}
	
	/**
	 * Puts in a value
	 * @param key The key to put it under
	 * @param value The value to give it
	 */
	public static void putInt(String key, int value)
	{
		prefs.putInt(key, value);
		flush();
	}
	
	/**
	 * Gets a value
	 * @param key The key to get the value based on
	 * @return The value, or false if key did not exist
	 */
	public static boolean getBoolean(String key)
	{
		return prefs.getBoolean(key, false);
	}
	
	/**
	 * Puts in a value
	 * @param key The key to put it under
	 * @param value The value to give it
	 */
	public static void putBoolean(String key, boolean value)
	{
		prefs.putBoolean(key, value);
		flush();
	}
	
	/**
	 * Gets a value
	 * @param key The key to get the value based on
	 * @return The value, or false if key did not exist
	 */
	public static double getDouble(String key)
	{
		return prefs.getDouble(key, -1.0);
	}
	
	/**
	 * Puts in a value
	 * @param key The key to put it under
	 * @param value The value to give it
	 */
	public static void putDouble(String key, double value)
	{
		prefs.putDouble(key, value);
		flush();
	}
	
	private static void flush() // helper
	{
		try
		{
			prefs.flush();
		} catch (BackingStoreException ex)
		{
			ex.printStackTrace();
		}
	}

}
